package cn.iocoder.yudao.module.system.service.businessliaison;

import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.businessliaison.BusinessLiaisonDO;
import cn.iocoder.yudao.module.system.dal.mysql.businessliaison.BusinessLiaisonMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业务接口人 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class BusinessLiaisonServiceImpl implements BusinessLiaisonService {

    @Resource
    private BusinessLiaisonMapper businessLiaisonMapper;


    /**
     * 获得业务接口人
     *
     * @param type     业务类型
     * @param tenantId 租户id
     * @param deptId   部门id
     * @param leaderId 领导人id
     * @return 业务接口人
     */
    @Override
    public Long getBusinessLiaison(Integer type, Long tenantId, Long deptId, Long leaderId) {

        MPJLambdaWrapperX<BusinessLiaisonDO> queryWrapper = new MPJLambdaWrapperX<>();
        queryWrapper.eqIfPresent(BusinessLiaisonDO::getBusinessType, type);
        queryWrapper.eqIfPresent(BusinessLiaisonDO::getTenantId, tenantId);
        queryWrapper.eqIfPresent(BusinessLiaisonDO::getDeptId, deptId);
        queryWrapper.eqIfPresent(BusinessLiaisonDO::getLeaderId, leaderId);

        List<BusinessLiaisonDO> businessLiaisonDOS = businessLiaisonMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(businessLiaisonDOS)) {
            // 没有配置，则返回领导人id
            return leaderId;
        }

        return businessLiaisonDOS.get(0).getLiaisonId();
    }

    /**
     * 判断业务接口人是否存在
     *
     * @param type     业务类型
     * @param tenantId 租户id
     * @param leaderId 领导人id
     * @return 是否存在
     */
    @Override
    public boolean exitBusinessLiaison(Integer type, Long tenantId, Long leaderId) {

        LambdaQueryWrapper<BusinessLiaisonDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BusinessLiaisonDO::getBusinessType, type);
        queryWrapper.eq(BusinessLiaisonDO::getTenantId, tenantId);
        queryWrapper.eq(BusinessLiaisonDO::getLeaderId, leaderId);

        List<BusinessLiaisonDO> businessLiaisonDOS = businessLiaisonMapper.selectList(queryWrapper);


        return !CollectionUtils.isEmpty(businessLiaisonDOS);
    }

    /**
     * 根据接口人找到leader
     *
     * @param type      业务类型
     * @param tenantId  租户id
     * @param liaisonId 接口人
     * @return 领导人id
     */
    @Override
    public Long getBusinessLeader(Integer type, Long tenantId, Long liaisonId) {

        LambdaQueryWrapper<BusinessLiaisonDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BusinessLiaisonDO::getBusinessType, type);
        queryWrapper.eq(BusinessLiaisonDO::getTenantId, tenantId);
        queryWrapper.eq(BusinessLiaisonDO::getLiaisonId, liaisonId);

        List<BusinessLiaisonDO> businessLiaisonDOS = businessLiaisonMapper.selectList(queryWrapper);

        if (CollectionUtils.isEmpty(businessLiaisonDOS)) {
            return null;
        }

        return businessLiaisonDOS.get(0).getLeaderId();
    }
}